package com.freemypay.test.并发.线程之间的协作.wait和notifyAll;

import java.math.BigDecimal;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 失败
 * @author yesixian
 * @date 2018年6月20日
 */
public class 练习22 {
	private static Car1 c = new Car1();
	
	public static void main(String[] args) throws InterruptedException {
		ExecutorService exec = Executors.newCachedThreadPool();
		exec.execute(new WaxOn1(c));
		exec.execute(new WaxOff1(c));
		TimeUnit.SECONDS.sleep(2);
		exec.shutdownNow();
	}

}

class Car1 {
	/**
	 * 标志
	 */
	private boolean flag = false;
	
	public synchronized void waxed() {
		flag = true;
	}
	
	public synchronized void waitForBuffing() {
		while(flag == false) {
			
		}
		flag = false;
	}
	
}

class WaxOn1 implements Runnable {
	private Car1 c;
	
	public WaxOn1(Car1 car1) {
		this.c = car1;
	}

	@Override
	public void run() {
		try {
			Thread.sleep(5);
			c.waxed();
		} catch (InterruptedException e) {
			System.out.println("InterruptedException on WaxOn class");
		}
		
	}
	
}

class WaxOff1 implements Runnable {
	private Car1 c;
	
	public WaxOff1(Car1 car1) {
		this.c = car1;
	}

	@Override
	public void run() {
		long star = System.currentTimeMillis();
		c.waitForBuffing();
		System.out.println("flag 改变了");
		long end = System.currentTimeMillis();
		long time = end - star;
		System.out.println("运行的时间毫秒数为：" + time);
	}
	
}